<!--
 * @Descripttion: (记录工件加工过程的详细信息/cks_production_record)
 * @Author: (admin)
 * @Date: (2025-06-24)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>

      <el-form-item label="盖板码" prop="coverCode">
        <el-input v-model="queryParams.coverCode" placeholder="" clearable style="width: 160px" />
      </el-form-item>
      <el-form-item label="产品码" prop="productCode">
        <el-input v-model="queryParams.productCode" placeholder="" clearable style="width: 240px" />
      </el-form-item>
      <el-form-item label="时间">
        <!--  :default-time="defaultTime2" -->
        <el-date-picker v-model="queryParams.dateRange" style="width: 360px" type="datetimerange"
          :default-time="defaultTime2" value-format="YYYY-MM-DD HH:mm:ss" range-separator="-" start-placeholder=""
          end-placeholder=""></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>

        <el-button type="warning" plain icon="Download" @click="handleExport" >
                    {{ $t('btn.export') }}
                    </el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <!-- <el-button type="primary" v-hasPermi="['cksproductionrecord:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button> -->
      </el-col>
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange">
      <el-table-column prop="serialNumber" label="流水号" width="150" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="coverCode" label="盖板码" width="160" align="center" :show-overflow-tooltip="true"/>
      <el-table-column prop="productCode" label="成品码" width="280" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="barcodeGrade" label="条码等级" width="120" align="center" :show-overflow-tooltip="true" >
        <template #default="scope">
          <span v-if="scope.row.barcodeGrade == '' "></span>
          <el-tag v-else-if="scope.row.barcodeGrade == 'F'" type="warning">{{ scope.row.barcodeGrade }}</el-tag>
          <el-tag v-else type="success"> {{ scope.row.barcodeGrade }} </el-tag>

        </template>
      </el-table-column>
      <el-table-column prop="processingStatus" label="加工状态" width="120" align="center"
        v-if="columns.showColumn('processingStatus')">
        <template #default="scope">
          <dict-tag :options="options.processingStatusOptions" :value="[scope.row.processingStatus]" />
        </template>
      </el-table-column>
      <el-table-column prop="aSideMachineNumber" label="A面机床编号" width="120" align="center" />
      <el-table-column prop="aSideToolingNumber" label="A面工装编号" width="120" align="center" />
      <el-table-column prop="aSideLoadingTime" label="A面投入时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="aSideStartTime" label="A面开始时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="aSideEndTime" label="A面结束时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="aSideUnloadingTime" label="A面取出时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="markingLoadingTime" label="打标投入时间" width="160" :show-overflow-tooltip="true" />
      <el-table-column prop="markingStartTime" label="打标开始时间" width="160" :show-overflow-tooltip="true" />
      <el-table-column prop="markingCompletedTime" label="打码完成时间" width="160" :show-overflow-tooltip="true" />
      <el-table-column prop="markingGradingTime" label="打标评级时间" width="160" :show-overflow-tooltip="true" />
      <el-table-column prop="bSideMachineNumber" label="B面的机床编号" width="120" align="center" />
      <el-table-column prop="bSideToolingNumber" label="B面的工装编号" width="120" align="center" />
      <el-table-column prop="bSideLoadingTime" label="B面投入时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="bSideStartTime" label="B面开始时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="bSideEndTime" label="B面完成时间" width="160" :show-overflow-tooltip="true"  />
      <el-table-column prop="bSideUnloadingTime" label="B面取出的时间" width="160" :show-overflow-tooltip="true" />
      <!-- <el-table-column prop="productCleaningLoadingTime" label="成品清洗投入时间" width="160" :show-overflow-tooltip="true"  /> -->


      <!-- <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['cksproductionrecord:edit']"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['cksproductionrecord:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改记录工件加工过程的详细信息对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <el-col :lg="12" v-if="opertype != 1">
            <el-form-item label="Id" prop="id">
              <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id"
                :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="加工流水号" prop="serialNumber">
              <el-input v-model="form.serialNumber" placeholder="请输入加工流水号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="盖板码" prop="coverCode">
              <el-input v-model="form.coverCode" placeholder="请输入盖板码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="成品码" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请输入成品码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="条码的质量等级" prop="barcodeGrade">
              <el-input v-model="form.barcodeGrade" placeholder="请输入条码的质量等级" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="加工状态 1-A面开始 2-A面完成 3-完成 4-打标投入 5-打标结束 6-B面开始" prop="processingStatus">
              <el-radio-group v-model="form.processingStatus">
                <el-radio v-for="item in options.processingStatusOptions" :key="item.dictValue"
                  :value="parseInt(item.dictValue)">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面的机床编号" prop="aSideMachineNumber">
              <el-input v-model.number="form.aSideMachineNumber" placeholder="请输入A面的机床编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面的工装编号" prop="aSideToolingNumber">
              <el-input v-model.number="form.aSideToolingNumber" placeholder="请输入A面的工装编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面投入时间" prop="aSideLoadingTime">
              <el-date-picker v-model="form.aSideLoadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面开始时间" prop="aSideStartTime">
              <el-date-picker v-model="form.aSideStartTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面结束时间" prop="aSideEndTime">
              <el-date-picker v-model="form.aSideEndTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="A面取出时间" prop="aSideUnloadingTime">
              <el-date-picker v-model="form.aSideUnloadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="打标清洗投入时间" prop="markingCleaningLoadingTime">
              <el-date-picker v-model="form.markingCleaningLoadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="打标开始时间" prop="markingStartTime">
              <el-date-picker v-model="form.markingStartTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="打码并完成评级的时间" prop="markingGradingTime">
              <el-date-picker v-model="form.markingGradingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="打标取出时间" prop="markingUnloadingTime">
              <el-date-picker v-model="form.markingUnloadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面的机床编号" prop="bSideMachine">
              <el-input v-model.number="form.bSideMachine" placeholder="请输入B面的机床编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面的工装编号" prop="bSideTooling">
              <el-input v-model.number="form.bSideTooling" placeholder="请输入B面的工装编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面投入时间" prop="bSideLoadingTime">
              <el-date-picker v-model="form.bSideLoadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面开始时间" prop="bSideStartTime">
              <el-date-picker v-model="form.bSideStartTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面完成时间" prop="bSideEndTime">
              <el-date-picker v-model="form.bSideEndTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="B面取出的时间" prop="bSideUnloadingTime">
              <el-date-picker v-model="form.bSideUnloadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="成品清洗投入时间" prop="productCleaningLoadingTime">
              <el-date-picker v-model="form.productCleaningLoadingTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="Nullity" prop="nullity">
              <el-input v-model="form.nullity" placeholder="请输入Nullity" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CreatedTime" prop="createdTime">
              <el-date-picker v-model="form.createdTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="IsDeleted" prop="isDeleted">
              <el-radio-group v-model="form.isDeleted">
                <el-radio v-for="item in options.isDeletedOptions" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cksproductionrecord">
import {
  listCksProductionRecord,
  addCksProductionRecord, delCksProductionRecord,
  updateCksProductionRecord, getCksProductionRecord,
}
  from '@/api/business/cksproductionrecord.js'
 import dayjs from 'dayjs'
 import request,{ downFile } from '@/utils/request'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const today = dayjs()
const startDay  = dayjs().subtract(3, 'day')

const defaultDateRange = [startDay.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
today.endOf('day').format('YYYY-MM-DD HH:mm:ss')]
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  dateRange:defaultDateRange,
   productCode:'',
   coverCode:''
})
const columns = ref([
  { visible: true, prop: 'id', label: 'Id' },
  { visible: true, prop: 'serialNumber', label: '加工流水号' },
  { visible: true, prop: 'coverCode', label: '盖板码' },
  { visible: true, prop: 'productCode', label: '成品码' },
  { visible: true, prop: 'barcodeGrade', label: '条码的质量等级' },
  { visible: true, prop: 'processingStatus', label: '加工状态 1-A面开始 2-A面完成 3-完成 4-打标投入 5-打标结束 6-B面开始' },
  { visible: true, prop: 'aSideMachineNumber', label: 'A面的机床编号' },
  { visible: true, prop: 'aSideToolingNumber', label: 'A面的工装编号' },
  { visible: false, prop: 'aSideLoadingTime', label: 'A面投入时间' },
  { visible: false, prop: 'aSideStartTime', label: 'A面开始时间' },
  { visible: false, prop: 'aSideEndTime', label: 'A面结束时间' },
  { visible: false, prop: 'aSideUnloadingTime', label: 'A面取出时间' },
  { visible: false, prop: 'markingLoadingTime', label: '打标清洗投入时间' },
  { visible: false, prop: 'markingStartTime', label: '打标开始时间' },
  { visible: false, prop: 'markingCompletedTime', label: '打标完成时间' },
  { visible: false, prop: 'markingGradingTime', label: '打标评级时间' },
  { visible: false, prop: 'bSideMachine', label: 'B面的机床编号' },
  { visible: false, prop: 'bSideTooling', label: 'B面的工装编号' },
  { visible: false, prop: 'bSideLoadingTime', label: 'B面投入时间' },
  { visible: false, prop: 'bSideStartTime', label: 'B面开始时间' },
  { visible: false, prop: 'bSideEndTime', label: 'B面完成时间' },
  { visible: false, prop: 'bSideUnloadingTime', label: 'B面取出的时间' },
  { visible: false, prop: 'productCleaningLoadingTime', label: '成品清洗投入时间' },
  { visible: false, prop: 'nullity', label: 'Nullity' },
  { visible: false, prop: 'createdTime', label: 'CreatedTime' },
  { visible: false, prop: 'isDeleted', label: 'IsDeleted' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
const defaultTime2 = [
    new Date(2000, 1, 1, 0, 0, 0),
    new Date(2000, 2, 1, 23, 59, 59),
] // '12:00:00', '08:00:00'

var dictParams = [
]



function getQueryBody()
{
    const { dateRange ,pageNum,pageSize,coverCode,productCode } = queryParams
    const body = { pageNum,pageSize,coverCode,productCode }
    proxy.addDateRange(body, dateRange) 
    return body
}


function getList() {
  loading.value = true
  const body = getQueryBody()
  listCksProductionRecord(body).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.dateRange = []
  const date = dayjs()
queryParams.dateRange = defaultDateRange
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    serialNumber: [{ required: true, message: "加工流水号不能为空", trigger: "blur" }],
    coverCode: [{ required: true, message: "盖板码不能为空", trigger: "blur" }],
    productCode: [{ required: true, message: "成品码不能为空", trigger: "blur" }],
    barcodeGrade: [{ required: true, message: "条码的质量等级不能为空", trigger: "blur" }],
    processingStatus: [{ required: true, message: "加工状态 ", trigger: "blur", type: "number" }],
    aSideMachineNumber: [{ required: true, message: "A面的机床编号不能为空", trigger: "blur", type: "number" }],
    aSideToolingNumber: [{ required: true, message: "A面的工装编号不能为空", trigger: "blur", type: "number" }],
    bSideMachine: [{ required: true, message: "B面的机床编号不能为空", trigger: "blur", type: "number" }],
    bSideTooling: [{ required: true, message: "B面的工装编号不能为空", trigger: "blur", type: "number" }],
    nullity: [{ required: true, message: "Nullity不能为空", trigger: "blur" }],
    createdTime: [{ required: true, message: "CreatedTime不能为空", trigger: "blur" }],
    isDeleted: [{ required: true, message: "IsDeleted不能为空", trigger: "blur" }],
  },
  options: {
    // 加工状态 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    processingStatusOptions: [
    { dictLabel: 'A面投入', dictValue: 1 ,listClass:'primary'},
    { dictLabel: 'A面开始', dictValue: 2 ,listClass:'primary'},
    { dictLabel: 'A面结束', dictValue: 3 ,listClass:'primary'},
    { dictLabel: 'A面取出', dictValue: 4 ,listClass:'primary'},
    { dictLabel: '打标投入', dictValue: 5 ,listClass:'primary'},
    { dictLabel: '打标开始', dictValue: 6 ,listClass:'primary'},
    { dictLabel: '打标完成', dictValue: 7 ,listClass:'primary'},
    { dictLabel: '打标评级', dictValue: 8 ,listClass:'primary'},
    { dictLabel: 'B面投入', dictValue: 9 ,listClass:'primary'},
    { dictLabel: 'B面开始', dictValue: 10 ,listClass:'primary'},
    { dictLabel: 'B面结束', dictValue: 11 ,listClass:'primary'},
    { dictLabel: 'B面取出', dictValue: 12 ,listClass:'primary'},
    { dictLabel: '完成', dictValue: 100 ,listClass:'success'},
    ],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    serialNumber: null,
    coverCode: null,
    productCode: null,
    barcodeGrade: null,
    processingStatus: null,
    aSideMachineNumber: null,
    aSideToolingNumber: null,
    aSideLoadingTime: null,
    aSideStartTime: null,
    aSideEndTime: null,
    aSideUnloadingTime: null,
    markingCleaningLoadingTime: null,
    markingStartTime: null,
    markingGradingTime: null,
    markingUnloadingTime: null,
    bSideMachine: null,
    bSideTooling: null,
    bSideLoadingTime: null,
    bSideStartTime: null,
    bSideEndTime: null,
    bSideUnloadingTime: null,
    productCleaningLoadingTime: null,
    nullity: null,
    createdTime: null,
    isDeleted: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加记录工件加工过程的详细信息'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksProductionRecord(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改记录工件加工过程的详细信息'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksProductionRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCksProductionRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksProductionRecord(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}




//导出，必须有查询条件 ，
const handleExport =async ()=>{
    const body1  = getQueryBody()
    console.log('body1',body1)
    const { coverCode,productCode ,beginTime ,endTime} = body1
    const body = {coverCode,productCode  , beginTime,endTime}
    const hasValue = Object.values(body).some(value => value!== undefined && value!== null && String(value).trim()!== '')
    if(!hasValue)
    {
        proxy.$modal.msgError('请至少输入1个查询条件')
        return
    }

    //提交导出
    await downFile('/business/CksProductionRecord/ExportData', { ...body })

}





handleQuery()
</script>